<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<title>会员列表</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport"
		content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
	<link rel="stylesheet" href="/resource/layuiadmin/layui/css/layui.css" media="all">
	<link rel="stylesheet" href="/resource/css/mylay.css">
	<link rel="stylesheet" href="/resource/css/agent_reward.css">
	<!-- <link rel="stylesheet" href="./layui/css/layui.css" media="all"> -->

</head>

<body>

	<!-- 表单元素 -->
	<div class="all">

		<div class="layui-tab">
			<ul class="layui-tab-title">
				<li class="layui-this" id="only">个人业绩奖励设置</li>
				<li id="some">团队业绩奖励设置</li>
			</ul>
			<div class="layui-tab-content">
				<div class="layui-tab-item layui-show geRenYeJi">
					<form class="layui-form" action="" lay-filter="example">
						<div class="layui-form-item">
							<label class="layui-form-label">个人业绩奖励</label>
							<div class="layui-input-inline">
								<input id="personage" type="radio" name="personage" value="1" title="开启">
								<input id="personage" type="radio" name="personage" value="0" title="关闭">
							</div>
							<div class="layui-input-block line_h36">开启后，个人业绩达到条件时，系统将发放对应的奖励</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">奖励发放模式</label>
							<div class="layui-input-inline">
								<input id="pattern" type="radio" name="pattern" value="1" title="自动">
								<input id="pattern" type="radio" name="pattern" value="2" title="手动">
							</div>
							<div class="layui-input-block line_h36">选中手动发放时，所有推荐奖励需手动点击发放后才发放；选中自动发放时，系统将自动发放对应的奖励</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">奖励结算周期</label>
							<div class="layui-input-inline">
								<input id="award" type="radio" name="award" value="1" title="天结">
								<input id="award" type="radio" name="award" value="2" title="周结">
								<input id="award" type="radio" name="award" value="3" title="月结">
							</div>
							<div class="layui-input-block line_h36">选择周结，则是以每周为单位结算一次，选择月结，则以每月为单位结算一次</div>
						</div>
						<div class="layui-form-item item_box">
							<div class="layui-form-item item inputItem">
								<div class="layui-inline">
									<label class="layui-form-label">任务金额</label>
									<div class="layui-input-inline" style="width: 130px;">
										<input id="price_min" type="number" name="price_min" lay-verify="required"
											placeholder="￥" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid">至</div>
									<div class="layui-input-inline" style="width: 130px;">
										<input id="price_max" type="number" name="price_max" lay-verify="required"
											placeholder="￥" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid">，奖励</div>
									<div class="layui-input-inline inp_before" style="width: 100px;">
										<input id="percent" type="number" name="percent" lay-verify="required"
											placeholder="" autocomplete="off" class="layui-input">
									</div>
								</div>
							</div>
							<div class="layui-form-mid box_btn">
								<button type="button" class="layui-btn btn insertFrom1">增加阶梯</button>
								<button type="button" class="layui-btn btn deleteFrom1">删除阶梯</button>
							</div>

						</div>
						<div class="layui-form-item text_center">
							<button type="button" class="layui-btn btn personageSubmit" lay-submit
								lay-filter="personageSubmit">保存</button>
						</div>
					</form>
				</div>
				<div class="layui-tab-item tuanDuiYeJi">
					<form class="layui-form" action="" lay-filter="example">
						<div class="layui-form-item">
							<label class="layui-form-label">团队业绩奖励</label>
							<div class="layui-input-inline">
								<input id="team" type="radio" name="team" value="1" title="开启">
								<input id="team" type="radio" name="team" value="0" title="关闭">
							</div>
							<div class="layui-input-block line_h36">开启后，团队业绩达到条件时，系统将发放对应的奖励</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">奖励发放模式</label>
							<div class="layui-input-inline">
								<input id="pattern" type="radio" name="pattern" value="1" title="自动">
								<input id="pattern" type="radio" name="pattern" value="2" title="手动">
							</div>
							<div class="layui-input-block line_h36">选中手动发放时，所有推荐奖励需手动点击发放后才发放；选中自动发放时，系统将自动发放对应的奖励</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">奖励结算周期</label>
							<div class="layui-input-inline">
								<input id="period" type="radio" name="period" value="1" title="天结">
								<input id="period" type="radio" name="period" value="2" title="周结">
								<input id="period" type="radio" name="period" value="3" title="月结">
							</div>
							<div class="layui-input-block line_h36">选择周结，则是以每周为单位结算一次，选择月结，则以每月为单位结算一次</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">团队级差奖励</label>
							<div class="layui-input-inline">
								<input id="range" type="radio" name="range" value="1" title="开启">
								<input id="range" type="radio" name="range" value="0" title="关闭">
							</div>
							<div class="layui-input-block line_h36">
								开启后，当团队业绩达到指定值时，系统将会计算级差奖励，级差奖励的发放模式、结算周期与团队业绩奖励保存一致级差业绩=（团队业绩的返佣比例-直属下级团队业绩的返佣比例）*直属下级的业绩团队业绩=自身业绩+直属下级的业绩
							</div>
						</div>
						<div class="layui-form-item item_box">
							<div class="layui-form-item item inputItem">
								<div class="layui-inline">
									<label class="layui-form-label">团队任务金额</label>
									<div class="layui-input-inline" style="width: 130px;">
										<input id="price_min" type="number" lay-verify="required" name="price_min"
											placeholder="￥" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid">至</div>
									<div class="layui-input-inline" style="width: 130px;">
										<input id="price_max" type="number" lay-verify="required" name="price_max"
											placeholder="￥" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid">，奖励</div>
									<div class="layui-input-inline inp_before" style="width: 100px;">
										<input id="percent" type="number" lay-verify="required" name="percent"
											placeholder="" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid checkbox">
										<input id="astrict" type="checkbox" value="2" name="like1[read]"
											lay-skin="primary" title="">
									</div>
									<div class="layui-form-mid">领取限制：个人业绩</div>
									<div class="layui-input-inline" style="width: 100px;">
										<input id="performance" type="number" lay-verify="required" name="performance"
											placeholder="" autocomplete="off" class="layui-input">
									</div>
									<div class="layui-form-mid">以上</div>
								</div>
							</div>
							<div class="layui-form-mid box_btn">
								<button type="button" class="layui-btn btn insertFrom2">增加阶梯</button>
								<button type="button" class="layui-btn btn deleteFrom2">删除阶梯</button>
							</div>
						</div>
						<div class="layui-form-item text_center">
							<button type="button" class="layui-btn btn" lay-submit lay-filter="teamSubmit">保存</button>
						</div>
					</form>
				</div>
			</div>
		</div>
	</div>

	<!-- <script src="./layui/layui.js"></script> -->
	<script src="/resource/layuiadmin/layui/layui.js"></script>
	<script src="/resource/js/manage/init_date.js"></script>
	<script src="/resource/js/manage/user.js"></script>
	<script>
		//注意：选项卡 依赖 element 模块，否则无法进行功能性操作
		layui.use(['form', 'element'], function () {
			var element = layui.element;
			var form = layui.form;
			var personageList = [];
			var teamList = [];
			//增加from1
			function insertFrom1() {
				let from1 = layui.$('.geRenYeJi .layui-form .item')[0]
				let div = document.createElement("div");
				div.classList.add('layui-inline')
				div.innerHTML =
					`<label class="layui-form-label">任务金额</label>
								<div class="layui-input-inline" style="width: 130px;">
									<input id="price_min" type="number" name="price_min" lay-verify="required" placeholder="￥" autocomplete="off" class="layui-input">
								</div>
								<div class="layui-form-mid">至</div>
								<div class="layui-input-inline" style="width: 130px;">
									<input id="price_max" type="number" name="price_max" lay-verify="required" placeholder="￥" autocomplete="off" class="layui-input">
								</div>
								<div class="layui-form-mid">，奖励</div>
								<div class="layui-input-inline inp_before" style="width: 100px;">
									<input id="percent" type="number" name="percent" lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
								</div>`
				from1.appendChild(div);
				form.render(); //刷新select选择框渲染
			};
			//增加from2
			function insertFrom2() {
				let from2 = layui.$('.tuanDuiYeJi .layui-form .item')[0]
				let div = document.createElement("div");
				div.classList.add('layui-inline')
				div.innerHTML =
					`<label class="layui-form-label">团队任务金额</label>
							<div class="layui-input-inline" style="width: 130px;">
								<input id="price_min" type="number" lay-verify="required" name="price_min" placeholder="￥" autocomplete="off" class="layui-input">
							</div>
							<div class="layui-form-mid">至</div>
							<div class="layui-input-inline" style="width: 130px;">
								<input id="price_max" type="number" lay-verify="required" name="price_max" placeholder="￥" autocomplete="off" class="layui-input">
							</div>
							<div class="layui-form-mid">，奖励</div>
							<div class="layui-input-inline inp_before" style="width: 100px;">
								<input id="percent" type="number" lay-verify="required" name="percent" placeholder="" autocomplete="off" class="layui-input">
							</div>
							<div class="layui-form-mid checkbox">
								<input id="astrict" type="checkbox" name="like1[read]" lay-skin="primary" title="">
							</div>
							<div class="layui-form-mid">领取限制：个人业绩</div>
							<div class="layui-input-inline" style="width: 100px;">
								<input id="performance" type="number" lay-verify="required" name="performance" placeholder="" autocomplete="off" class="layui-input">
							</div>
							<div class="layui-form-mid">以上</div>`;
				from2.appendChild(div);
				form.render(); //刷新select选择框渲染
			};
			// 个人业绩
			function insertPersonage() {
				layui.$(".geRenYeJi [id='personage']")[personageList.is_on == 1 ? 0 : 1].checked = true;
				layui.$(".geRenYeJi [id='pattern']")[personageList.send_mode == 1 ? 0 : 1].checked = true;
				layui.$(".geRenYeJi [id='award']")[personageList.reward_period-1].checked = true;
				console.log(personageList.reward_config.length)
				for (let i = 0; i < personageList.reward_config.length; i++) {
					if (i < personageList.reward_config.length - 1) {
						insertFrom1()
					}
					layui.$('.geRenYeJi .item>.layui-inline #price_min')[i].value = personageList.reward_config[i].min
					layui.$('.geRenYeJi .item>.layui-inline #price_max')[i].value = personageList.reward_config[i].max
					layui.$('.geRenYeJi .item>.layui-inline #percent')[i].value = personageList.reward_config[i].value
				}
			};

			function insertTeam() {
				// 团队业绩
				layui.$(".tuanDuiYeJi [id='team']")[teamList.is_on == 1 ? 0 : 1].checked = true;
				layui.$(".tuanDuiYeJi [id='pattern']")[teamList.send_mode == 1 ? 0 : 1].checked = true;
				layui.$(".tuanDuiYeJi [id='period']")[teamList.reward_period-1].checked = true;
				layui.$(".tuanDuiYeJi [id='range']")[teamList.is_team_on == 1 ? 0 : 1].checked = true;
				for (let i = 0; i < teamList.reward_config.length; i++) {
					if (i < teamList.reward_config.length - 1) {
						insertFrom2()
					}
					layui.$('.tuanDuiYeJi .item>.layui-inline #price_min')[i].value = teamList.reward_config[i].min
					layui.$('.tuanDuiYeJi .item>.layui-inline #price_max')[i].value = teamList.reward_config[i].max
					layui.$('.tuanDuiYeJi .item>.layui-inline #percent')[i].value = teamList.reward_config[i].value
					layui.$('.tuanDuiYeJi .item>.layui-inline #performance')[i].value = teamList.reward_config[i]
						.amountLimit
					layui.$('.tuanDuiYeJi .item>.layui-inline #astrict')[i].checked = teamList.reward_config[i]
						.mode == 1 ? false : true
				}
			};
			// 获取数据
			function getList() {
				layui.$.ajax({
					url: '/manage/userAgent/agentReward',
					type: 'get',
					success: function (data) {
						if (data.data[0]){
							data.data[0].reward_config = JSON.parse(data.data[0].reward_config)
							if (data.data[1])
								data.data[1].reward_config = JSON.parse(data.data[1].reward_config)
							if (data.data[0].reward_type == 1) {
								personageList = data.data[0]
							} else {
								personageList = data.data[1]
							}
							if (data.data[0].reward_type == 2) {
								teamList = data.data[0]
							} else {
								teamList = data.data[1]
							}
							insertPersonage();
							form.render(); //刷新select选择框渲染
							if (data.data[1])
								insertTeam()
							form.render(); //刷新select选择框渲染
						}
					},
					error: function (e) {
						layer.alert("请求失败！")
					},
				});
			}
			getList();
			// for(let i=0;i<3-1;i++){
			// 	if(i<1){
			// 		insertFrom1()
			// 	}
			// 	layui.$('.geRenYeJi .item>.layui-inline #price_min')[i].value=1
			// 	layui.$('.geRenYeJi .item>.layui-inline #price_max')[i].value=2

			// }
			// form.render(); //刷新select选择框渲染
			// 个人业绩奖励设置
			(function () {
				// 删除from1
				function deleteFrom1() {
					let from1 = layui.$('.geRenYeJi .layui-form .item')[0]
					let item = layui.$('.geRenYeJi .layui-form .item>div')[layui.$(
						'.geRenYeJi .layui-form .item>div').length - 1]

					from1.removeChild(item);
				}
				// from1
				//添加数据
				layui.$('.insertFrom1').on('click', function () {
					insertFrom1()
				});
				//删除数据
				layui.$('.deleteFrom1').on('click', function () {
					deleteFrom1()
				});

				//提交数据
				//监听提交
				form.on('submit(personageSubmit)', function (data) {
					let personage = layui.$("[id='personage']:checked").val(); //业绩
					let pattern = layui.$("[id='pattern']:checked").val(); //模式
					let award = layui.$("[id='award']:checked").val(); //周期
					let price_minList = layui.$('.geRenYeJi .item>.layui-inline #price_min')
					for (let i = 0; i < price_minList.length; i++) {
						price_minList[i] = price_minList[i].value
					}
					let price_maxList = layui.$('.geRenYeJi .item>.layui-inline #price_max')
					for (let i = 0; i < price_maxList.length; i++) {
						price_maxList[i] = price_maxList[i].value
					}
					let percentList = layui.$('.geRenYeJi .item>.layui-inline #percent')
					for (let i = 0; i < percentList.length; i++) {
						percentList[i] = percentList[i].value
					}
					let reward_config = []
					for (let i = 0; i < price_minList.length; i++) {
						reward_config[i] = {
							min: price_minList[i] * 1,
							max: price_maxList[i] * 1,
							mode: 1,
							value: percentList[i] * 1
						}
					}
					let json = {
						reward_type: 1,
						is_on: personage * 1,
						send_mode: pattern * 1,
						reward_period: award * 1,
						reward_config: reward_config
					}
					layui.$.ajax({
						url: '/manage/userAgent/agentReward?reward_type=1',
						type: 'post',
						data: json,
						success: function (data) {
							if (data.code == 1) {
								layer.alert("提交成功！")
							} else {
								layer.alert("提交失败！")
							}
						},
						error: function (e) {
							layer.alert("提交失败！")
						},
					});

				});
			}());

			// 团队业绩奖励设置
			(function () {
				// 删除from2
				function deleteFrom2() {
					let from2 = layui.$('.tuanDuiYeJi .layui-form .item')[0]
					let item = layui.$('.tuanDuiYeJi .layui-form .item>div')[layui.$(
						'.tuanDuiYeJi .layui-form .item>div').length - 1]
					from2.removeChild(item);
				}
				// from1
				//添加数据
				layui.$('.insertFrom2').on('click', function () {
					insertFrom2()
				});
				//删除数据
				layui.$('.deleteFrom2').on('click', function () {
					deleteFrom2()
				});
				//提交数据
				//监听提交
				form.on('submit(teamSubmit)', function (data) {
					let team = layui.$("[id='team']:checked").val(); //业绩
					let pattern = layui.$("[id='pattern']:checked").val(); //模式
					let period = layui.$("[id='period']:checked").val(); //周期
					let range = layui.$("[id='range']:checked").val(); //极差

					let price_minList = layui.$('.tuanDuiYeJi .item>.layui-inline #price_min')
					for (let i = 0; i < price_minList.length; i++) {
						price_minList[i] = price_minList[i].value
					}
					let price_maxList = layui.$('.tuanDuiYeJi .item>.layui-inline #price_max')
					for (let i = 0; i < price_maxList.length; i++) {
						price_maxList[i] = price_maxList[i].value
					}
					let percentList = layui.$('.tuanDuiYeJi .item>.layui-inline #percent')
					for (let i = 0; i < percentList.length; i++) {
						percentList[i] = percentList[i].value
					}
					let performanceList = layui.$('.tuanDuiYeJi .item>.layui-inline #performance')
					for (let i = 0; i < performanceList.length; i++) {
						performanceList[i] = performanceList[i].value
					}
					let astrictList = layui.$('.tuanDuiYeJi .item>.layui-inline #astrict')
					for (let i = 0; i < astrictList.length; i++) {
						astrictList[i] = astrictList[i].checked ? 2 : 1
					}
					let reward_config = []
					for (let i = 0; i < price_minList.length; i++) {
						reward_config[i] = {
							min: price_minList[i] * 1,
							max: price_maxList[i] * 1,
							mode: astrictList[i],
							value: percentList[i] * 1,
							amountLimit: performanceList[i] * 1
						}
					}
					let json = {
						reward_type: 2,
						is_on: team * 1,
						send_mode: pattern * 1,
						reward_period: period * 1,
						is_team_on: range * 1,
						reward_config: reward_config
					}
					console.log(json)
					layui.$.ajax({
						url: '/manage/userAgent/agentReward?reward_type=2',
						type: 'post',
						data: json,
						success: function (data) {
							if (data.code == 1) {
								layer.alert("提交成功！")
							} else {
								layer.alert("提交失败！")
							}
						},
						error: function (e) {
							layer.alert("提交失败！")
						},
					});

				});
			}())

			var only = document.getElementById('only');
			var some = document.getElementById('some');
			only.onclick = function () {
				this.style.backgroundColor = 'white';

				some.style.backgroundColor = 'rgb(242,242,242)';
				some.style.color = 'black'
			}
			some.onclick = function () {
				this.style.backgroundColor = 'white';

				only.style.backgroundColor = 'rgb(242,242,242)';
				only.style.color = 'black'
			}

		});
	</script>



</body>

</html>
